package com.bz.service.impl;

import com.bz.dao.UserBuyerDao;
import com.bz.entity.UserBuyer;
import com.bz.service.UserBuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;


@Service
@Transactional
public class UserBuyerServiceImpl implements UserBuyerService {

    @Autowired
    private UserBuyerDao userBuyerDao;

    @Override
    public void register(UserBuyer user) {

        UserBuyer u = userBuyerDao.findByUsername(user.getUsername());
        if (u != null) {
            throw new RuntimeException("用户名已存在");
        }
        user.setStatus("已激活");
        user.setRegdate(new Date());
        userBuyerDao.save(user);
    }

    @Override
    public UserBuyer login(UserBuyer user) {

        UserBuyer u = userBuyerDao.findByUsername(user.getUsername());
        if (!ObjectUtils.isEmpty(u)) {
            if ( u.getPassword().equals(user.getPassword())){
                return u;
            } else{
                throw  new RuntimeException("密码不正确");
            }
        } else {
            throw  new RuntimeException("用户不存在");
        }
    }

}
